﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace kompot_wtf
{
    public class GameHud
    {
        public TimeSpan TimeAlive { get; private set; }

        private SpriteFont textFont;
        private SpriteFont timeFont;

        private Texture2D lifeBar;

        private float life;

        public GameHud()
        {
            this.TimeAlive = TimeSpan.Zero;
            this.life = 100.0f;
        }

        public void Load()
        {
            textFont = WTF.ContentMgr.Load<SpriteFont>("Fonts//default");
            timeFont = WTF.ContentMgr.Load<SpriteFont>("Fonts//time");

            lifeBar = WTF.ContentMgr.Load<Texture2D>("Textures//lifebar");
        }

        public void AddTimeAlive(TimeSpan deltaTime)
        {
            TimeAlive += deltaTime;
        }

        public void UpdateLife(float life)
        {
            this.life = life;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();

            var timeText = string.Format(
                "{0:00}:{1:00}.{2:00}",
                TimeAlive.Minutes,
                TimeAlive.Seconds,
                TimeAlive.Milliseconds / 10);
            var timePosition = new Vector2(80, 15);

            var textTime = "TIME :";
            var textTimePosition = new Vector2(10, 20);

            var textLife = "LIFE : ";
            var textLifePosition = new Vector2(380, 20);

            var lifeBarModifier = life > 0 ? life / 100 : 0;
            var lifePosition = new Vector2(450, 20);
            var lifeRect = new Rectangle(0, 0, (int)(300 * lifeBarModifier), 20);

            spriteBatch.DrawString(textFont, textTime, textTimePosition, Color.Red);
            spriteBatch.DrawString(timeFont, timeText, timePosition, Color.Red);

            spriteBatch.DrawString(textFont, textLife, textLifePosition, Color.Red);
            spriteBatch.Draw(lifeBar, lifePosition, lifeRect, Color.White);

            spriteBatch.End();
        }

        public void DrawGameOver(SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();

            var text = "GAME OVER";
            var textPosition = new Vector2(100, 200);

            spriteBatch.DrawString(textFont, text, textPosition, Color.DarkRed);

            spriteBatch.End();
        }

        public void ResetTimeAlive()
        {
            this.TimeAlive = TimeSpan.Zero;
        }
    }
}
